import Api from '../service/Api';
import QueryString from '../component/QueryString';

const userInfo = JSON.parse(localStorage.getItem('yxtUser'));

export function loadNewsList(pageNo) {
    Api.get("/EduPlate/MSGAppIndex/InterfaceJson.asmx/List_GetList", {
        Page: pageNo,
        PageSize: 7,
        Unit_ID: userInfo.Unit_ID
    }).then((resp) => {
        console.log(resp);
        if (resp && resp.Goodo && resp.Goodo.R) {
        	const baseUrl = API.baseUrl;
            // 列表部分
            const ul = "<ul class='list'>" + resp.Goodo.R.map(item => {
            	let img = '';
            	if (item.PicUrl) {
            		return `<li>
	                <a class="open-detail" data-id="${item.Contents_ID}">
	                <img src="${baseUrl}${item.PicUrl}"/>
	                    <p class="title">${item.ContentTitle}</p>
	                    <p><span>${item.SubjectName}</span>${moment(item.SubmitDate, 'YYYY/M/D HH:mm:ss').format('YYYY-MM-DD')}</p>
	                </a>
	                </li>`;
            	} else {
            		return `<li class="on">
	                <a class="open-detail" data-id="${item.Contents_ID}">
	                <img src="${item.PicUrl}"/>
	                    <p class="title">${item.ContentTitle}</p>
	                    <p><span>${item.SubjectName}</span>${moment(item.SubmitDate, 'YYYY/M/D HH:mm:ss').format('YYYY-MM-DD')}</p>
	                </a>
	                </li>`;
            	}
            }).join('') + "</ul>";
            // 分页器部分
            const {PageCount}  = resp.Goodo;
            let navList = "";
            if (PageCount <= 3) {
              for(let i = 1; i <= PageCount; i++) {
                if (pageNo === i) {
                  navList += `<li><a data-page="${i}" class="navbtn active">${i}</a></li>`
                } else {
                  navList += `<li><a data-page="${i}" class="navbtn">${i}</a></li>`
                }
              }
            } else if (pageNo + 2 >= PageCount) {
              for(let i = PageCount - 2; i <= PageCount; i++) {
                if (pageNo === i) {
                  navList += `<li><a data-page="${i}" class="navbtn active">${i}</a></li>`
                } else {
                  navList += `<li><a data-page="${i}" class="navbtn">${i}</a></li>`
                }
              }
            } else {
              navList = `
                <li><a data-page="${i}" class="navbtn">${pageNo - 1}</a></li>
                <li><a data-page="${i}" class="navbtn active">${pageNo}</a></li>
                <li><a data-page="${i}" class="navbtn">${pageNo + 1}</a></li>
              `
            }
            const nav = `
              <nav aria-label="Page navigation">
                <ul class="pagination">
                  <li>
                    <a class="pageFirst ${pageNo == 1 ? 'disable' : ''}" aria-label="First">
                      <span aria-hidden="true">&laquo;</span>
                    </a>
                  </li>
                  <li>
                    <a class="pagePrevious ${pageNo == 1 ? 'disable' : ''}" aria-label="Previous">
                      <span aria-hidden="true">&lt;</span>
                    </a>
                  </li>
                  ${navList}
                  <li>
                    <a class="pageNext ${pageNo == PageCount ? 'disable' : ''}" aria-label="Next">
                      <span aria-hidden="true">&gt;</span>
                    </a>
                  </li>
                  <li>
                    <a class="pageLast ${pageNo == PageCount ? 'disable' : ''}" aria-label="Last">
                      <span aria-hidden="true">&raquo;</span>
                    </a>
                  </li>
                </ul>
              </nav>
            `

            $('.right').html(`<div class="top">
                <p>新闻列表</p>
                <a href="#" class="back">
                    <img src="img/news_public/news_sys.png" />返回
                </a>
            </div>
            <div class="cent">
              ${ul}${nav}
            </div>`);


            list_event(pageNo, PageCount);



        }
    });
}

function list_event(currPage, totalPage) {
  $(".right .open-detail").click(function() {
      loadNewsDetail($(this).data('id'));

      return false;
  });

  $(".right .back").click(function () {
      return false;
  });

  $(".pageFirst").click(function() {
      loadNewsList(1);
      return false;
  });
  $(".pageLast").click(function() {
      loadNewsList(totalPage);
      return false;
  });
  $(".pagePrevious").click(function() {
      loadNewsList(currPage - 1);
      return false;
  });
  $(".pageNext").click(function() {
      loadNewsList(currPage + 1);
      return false;
  });

  $(".navbtn").click(function() {
      loadNewsList($(this).data("page"));
      return false;
  })

}


export function loadNewsDetail(id) {
    Api.get("/web/wsjson.asmx/Get_Json_Content", {
        Content_ID: id
    }).then((resp) => {
        const record = resp.Goodo.Record;
        console.log(moment(record.SubmitDate, 'YYYY/M/D HH:mm:ss').format('YYYY-MM-DD'))
console.log(record)
        const tpl = `
        <div class="top">
            <p>新闻详情</p>
            <a href="#" class="back">
               <img src="img/news_public/news_sys.png" />返回
            </a>
        </div>
        <div class="centra">
            <h4>${record.ContentTitle}</h4>
			<p class="title">时间: <span>${moment(record.SubmitDate, 'YYYY/M/D HH:mm:ss').format('YYYY-MM-DD')}</span>发布人：<span>${record.UserName}</span>浏览次数：<span>${record.ICount}</span></p>
            <p>${record.Content.replace("\n", "</p><p>")}</p>
        </div>
        `;

        $(".right").html(tpl);

        $(".right .back").click(function () {
            loadNewsList();
            return false;
        });
    });
}
